package com.zhongxi.guoyuan.adapter.personCenter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.user.center.BeanDynamicResult;
import com.zhongxi.guoyuan.utils.DataUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/1/18.
 */

public class BeanDynamicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context context;
    private List<BeanDynamicResult.BeanDynamicData> data;
    private int footer_state = 0;

    //普通布局
    static final int TYPE_ITEM = 0;
    //底部布局
    static final int TYPE_FOOTER = 1;

    //正在加载更多
    static final int LOAD_MORE = 1;

    //没有更多
    static final int NO_MORE = 2;

    //初始状态
    static final int INIT = 0;


    public BeanDynamicAdapter(Context context) {
        this.context = context;
        data = new ArrayList<>();
    }

    public void setData(List<BeanDynamicResult.BeanDynamicData> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void setFooter_state(int footer_state) {
        this.footer_state = footer_state;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.bean_dynamic_layout, parent, false);
            RecycleViewHolder viewHolder = new RecycleViewHolder(view);
            return viewHolder;

        } else if (viewType == TYPE_FOOTER) {
            View view = View.inflate(context, R.layout.recycle_footer_layout, null);
            FooterViewHolder footerViewHolder = new FooterViewHolder(view);
            return footerViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof RecycleViewHolder) {
            if (position % 2 == 0) {
                ((RecycleViewHolder) holder).layoutBeanDynamic.setBackgroundColor(context.getResources().getColor(R.color.background));
            } else {
                ((RecycleViewHolder) holder).layoutBeanDynamic.setBackgroundColor(context.getResources().getColor(R.color.white));
            }
            BeanDynamicResult.BeanDynamicData beanDynamicData = data.get(position);
            if (null != beanDynamicData && !"".equals(beanDynamicData)) {
                if (null != beanDynamicData.getCtime() && !"".equals(beanDynamicData.getCtime())) {
                    ((RecycleViewHolder) holder).beanDynamicDate.setText(beanDynamicData.getCtime());
                } else {
                    ((RecycleViewHolder) holder).beanDynamicDate.setText("");
                }
                if (null != beanDynamicData.getUsetype() && !"".equals(beanDynamicData.getUsetype())) {
                    if (beanDynamicData.getUsetype().equals("1")) {

                        if (null != beanDynamicData.getRedbeannum() && !"".equals(beanDynamicData.getRedbeannum())&& DataUtil.moreThanZero(beanDynamicData.getRedbeannum())) {
                            ((RecycleViewHolder) holder).beanDynamicHongdounum.setVisibility(View.VISIBLE);

                            ((RecycleViewHolder) holder).beanDynamicHongdounum.setText("+ " + beanDynamicData.getRedbeannum()+"红豆");
                        }else {
                            ((RecycleViewHolder) holder).beanDynamicHongdounum.setVisibility(View.GONE);
                        }

                        if (null != beanDynamicData.getInnernum() && !"".equals(beanDynamicData.getInnernum())&&DataUtil.moreThanZero(beanDynamicData.getInnernum())){
                            ((RecycleViewHolder) holder).beanDynamicMeidounum.setVisibility(View.VISIBLE);

                            ((RecycleViewHolder) holder).beanDynamicMeidounum.setText("+ " + beanDynamicData.getInnernum()+"美豆");
                        }else {
                            ((RecycleViewHolder) holder).beanDynamicMeidounum.setVisibility(View.GONE);
                        }

                    } else {

                        if (null != beanDynamicData.getRedbeannum() && !"".equals(beanDynamicData.getRedbeannum())&& DataUtil.moreThanZero(beanDynamicData.getRedbeannum())) {
                            ((RecycleViewHolder) holder).beanDynamicHongdounum.setVisibility(View.VISIBLE);

                            ((RecycleViewHolder) holder).beanDynamicHongdounum.setText("- " + beanDynamicData.getRedbeannum()+"红豆");
                        }else {
                            ((RecycleViewHolder) holder).beanDynamicHongdounum.setVisibility(View.GONE);
                        }

                        if (null != beanDynamicData.getInnernum() && !"".equals(beanDynamicData.getInnernum())&&DataUtil.moreThanZero(beanDynamicData.getInnernum())){
                            ((RecycleViewHolder) holder).beanDynamicMeidounum.setVisibility(View.VISIBLE);

                            ((RecycleViewHolder) holder).beanDynamicMeidounum.setText("- " + beanDynamicData.getInnernum()+"美豆");
                        }else {
                            ((RecycleViewHolder) holder).beanDynamicMeidounum.setVisibility(View.GONE);
                        }

                    }
                }
                if (null != beanDynamicData.getCtype() && !"".equals(beanDynamicData.getCtype())) {
                    String ctype = beanDynamicData.getCtype();
                    switch (ctype) {
                        case "1":
                            ((RecycleViewHolder) holder).beanDynamicArea.setText("线上赚豆超市");
                            break;
                        case "2":
                            ((RecycleViewHolder) holder).beanDynamicArea.setText("面对面扫码付");
                            break;
                        case "3":
                            ((RecycleViewHolder) holder).beanDynamicArea.setText("线下赚豆商家");
                            break;
                        case "4":
                            ((RecycleViewHolder) holder).beanDynamicArea.setText("快乐分享");
                            break;
                        case "5":
                            ((RecycleViewHolder) holder).beanDynamicArea.setText("系统赠送");
                            break;
                        case "6":
                            ((RecycleViewHolder) holder).beanDynamicArea.setText("美豆美容");
                            break;
                        case "7":
                            ((RecycleViewHolder) holder).beanDynamicArea.setText("美豆兑换消费");
                            break;

                    }

                }


            } else if (holder instanceof FooterViewHolder) {

                switch (footer_state) {
                    case LOAD_MORE://2
                        ((ClearWeedExchangeAdapter.FooterViewHolder) holder).recycleProgressbar.setVisibility(View.VISIBLE);
                        ((ClearWeedExchangeAdapter.FooterViewHolder) holder).recycleTvLine1.setVisibility(View.GONE);
                        ((ClearWeedExchangeAdapter.FooterViewHolder) holder).recycleTvLine2.setVisibility(View.GONE);
                        ((ClearWeedExchangeAdapter.FooterViewHolder) holder).recycleFootTv.setVisibility(View.VISIBLE);
                        ((ClearWeedExchangeAdapter.FooterViewHolder) holder).recycleFootTv.setText("正在加载...");
                        break;
                    case INIT://上拉加载
                        ((ClearWeedExchangeAdapter.FooterViewHolder) holder).recycleProgressbar.setVisibility(View.GONE);
                        ((ClearWeedExchangeAdapter.FooterViewHolder) holder).recycleFootTv.setVisibility(View.GONE);
                        ((ClearWeedExchangeAdapter.FooterViewHolder) holder).recycleTvLine1.setVisibility(View.GONE);
                        ((ClearWeedExchangeAdapter.FooterViewHolder) holder).recycleTvLine2.setVisibility(View.GONE);
                        break;
                }
            }
        }

    }

    @Override
    public int getItemCount() {
        return data != null ? data.size() + 1 : 0;
    }

    static class RecycleViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.bean_dynamic_date)
        TextView beanDynamicDate;
        @Bind(R.id.bean_dynamic_area)
        TextView beanDynamicArea;
        @Bind(R.id.layout_bean_dynamic)
        LinearLayout layoutBeanDynamic;
        @Bind(R.id.bean_dynamic_hongdounum)
        TextView beanDynamicHongdounum;
        @Bind(R.id.bean_dynamic_meidounum)
        TextView beanDynamicMeidounum;

        RecycleViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    class FooterViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.recycle_tv_line1)
        TextView recycleTvLine1;
        @Bind(R.id.recycle_progressbar)
        ProgressBar recycleProgressbar;
        @Bind(R.id.recycle_foot_tv)
        TextView recycleFootTv;
        @Bind(R.id.recycle_tv_line2)
        TextView recycleTvLine2;
        @Bind(R.id.recycle_container_relative)
        RelativeLayout recycleContainerRelative;

        public FooterViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
